package com.liuanbang.saro.sarodemo.ui.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.base.AbstractDialog;
import com.liuanbang.saro.sarodemo.ui.popup.CommonChoosePopupWindow;
import com.liuanbang.saro.sarodemo.util.ToastUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TipActivity extends AppCompatActivity {
    @Bind(R.id.button_tip_activity_popupWindow)
    Button buttonTipActivityPopupWindow;
    @Bind(R.id.linearLayout_tip)
    LinearLayout linearLayoutTip;
    private PopupWindow popupWindow;
    private ArrayList<String> list_city = new ArrayList<>();

    private AbstractDialog abstractDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tip);
        ButterKnife.bind(this);
        getPopupWindow();

        for (int i = 0; i < 10; i++) {
            list_city.add("城市" + i);
        }

        abstractDialog = new CommonChoosePopupWindow(this, list_city, new AbstractDialog.OnDialogItemSelectListener() {
            @Override
            public void onDialogItemSelect(int type, String desc, String value) {
                ToastUtils.showToast("选择了");
            }
        });
    }

    public void clickPopupWindow1(View view) {
        popupWindow.showAtLocation(linearLayoutTip, Gravity.CENTER, 0, 0);
    }

    /**
     * 创建PopupWindow
     */
    protected void initPopupWindow() {
        // TODO: 15/10/9
        //获取自定义布局文件activity_pop_left.xml 布局文件
        final View popipWindow_view = getLayoutInflater().inflate(R.layout.activity_pop_left, null, false);
        //创建Popupwindow 实例，200，LayoutParams.MATCH_PARENT 分别是宽高
        popupWindow = new PopupWindow(popipWindow_view, 250, ViewGroup.LayoutParams.MATCH_PARENT, true);
//设置动画效果
        // popupWindow.setAnimationStyle(R.style.AnimationFade);
        //点击其他地方消失
        popipWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popipWindow_view != null && popipWindow_view.isShown()) {

                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
      /*  Button button1 = (Button) popipWindow_view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "当前点击的是我", Toast.LENGTH_SHORT).show();
                System.out.println("我被点击了");
            }
        });*/
    }

    /**
     * 获取PopipWinsow实例
     */
    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopupWindow();
        }
    }


    public void clickPopupWindow2(View view) {

    }
}
